const app = getApp()
Page({
  data: {
    searchKeyword: '',
    currentSpecialty: '',
    currentHospital: '',
    currentZhicheng: '',
    
    // 筛选选项
    specialties: ['心血管内科', '神经外科', '儿科', '妇产科', '骨科', '眼科'],
    hospitals: ['北京协和医院', '上海瑞金医院', '广州中山医院', '华西医院', '湘雅医院'],
    zhichengs: ['主治医师', '副主任医师', '主任医师'],
    
    // 分页相关
    pageSize: 10,          // 每页数量
    currentPage: 1,        // 当前页码
    totalPages: 1,         // 总页数
    isLoading: false,      // 是否正在加载
    hasMore: true,         // 是否有更多数据
    
    // 专家数据
    allExperts: [],        // 所有专家数据（模拟用）
    experts: [],           // 当前显示的专家数据
    filteredExperts: []    // 筛选后的专家列表
  },
  
  onLoad: function() {
	  //获取科室
	  this.getkeshi()
	  //获取医院
	  this.gethospital()
    
    // 初始化加载第一页
    this.loadExperts();
  },
  getkeshi:function(){
	  var that = this
	  app.util.request({
	  	url: 'special/keshi',
	  	data: {
	  		
	  	},
	  	success(res) {
	  		var data = res.data.data;
	  		var list = data.keshi
	  		that.setData({
	  		  specialties: list || that.data.specialties
	  		});
	  		// wx.setStorageSync('newlist',list)
	  	}
	  })
  },
  gethospital:function(){
	  var that = this
	  app.util.request({
	  	url: 'special/hospital',
	  	data: {
	  		
	  	},
	  	success(res) {
	  		var data = res.data.data;
	  		var list = data.hospital
	  		that.setData({
	  		  hospitals: list || that.data.hospitals
	  		});
	  	}
	  })
  },
  clearSearch:function(){
	  this.setData({
	    currentHospital:'',
		currentSpecialty:'',
		currentZhicheng:'',
		searchKeyword:'',
		currentPage:1
	  });
	  this.filterExperts(true)
  },
  
  // 加载专家数据（分页）
  loadExperts: function() {
    if (this.data.isLoading || !this.data.hasMore) return;
    const { currentPage, pageSize } = this.data;
    this.setData({ isLoading: true });
	var that = this
	app.util.request({
		url: 'special/listsindex',
		data: {
			keyword: that.data.searchKeyword,
			zhicheng:that.data.currentZhicheng,
			hospital:that.data.currentHospital,
			special:that.data.currentSpecialty,
			page_no:that.data.currentPage,
			page_size:that.data.pageSize
		},
		success(res) {			
			const start = (currentPage - 1) * pageSize;
			const end = start + pageSize;
			var data = res.data.data;
			var list = data.special || []
			that.setData({
			  filteredExperts: [...that.data.filteredExperts,...list],
			  currentPage: currentPage + 1,
			  hasMore: end < data.total,
			  isLoading: false
			});
		}
	})
  },
  
  // 搜索输入
  inputSearch: function(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
    this.filterExperts(true);
  },
  
  // 选择擅长领域
  changeSpecialty: function(e) {
    const specialty = this.data.specialties[e.detail.value];
    this.setData({
      currentSpecialty: specialty === '' ? '' : specialty,
      currentPage: 1,  // 重置页码
	  filteredExperts:[]
    });
    this.filterExperts(true);
  },
  
  // 选择出诊医院
  changeHospital: function(e) {
    const hospital = this.data.hospitals[e.detail.value];
    this.setData({
      currentHospital: hospital === '' ? '' : hospital,
      currentPage: 1,  // 重置页码
	  filteredExperts:[]
    });
    this.filterExperts(true);
  },
  
  // 选择专家职称
  changeZhicheng: function(e) {
    const zhicheng = this.data.zhichengs[e.detail.value];
    this.setData({
      currentZhicheng: zhicheng === '' ? '' : zhicheng,
      currentPage: 1,  // 重置页码
	  filteredExperts:[]
    });
    this.filterExperts(true);
  },
  // 筛选专家
  filterExperts: function(resetPage = false) {
    const { searchKeyword, currentSpecialty, currentHospital,currentZhicheng, experts } = this.data;
    
    if (resetPage) {
      // 重置分页状态
      this.setData({
        currentPage: 1,
        hasMore: true,
        filteredExperts: []
      });
      
      // 重新加载第一页
      this.loadExperts();
      return;
    }
    
    const filtered = experts.filter(expert => {
      // 搜索关键词匹配
      const keywordMatch = !searchKeyword || 
        expert.name.includes(searchKeyword) || 
        expert.department.includes(searchKeyword) ||
        expert.specialty.includes(searchKeyword);
      
      // 擅长领域匹配
      const specialtyMatch = !currentSpecialty || 
        expert.specialty.includes(currentSpecialty);
      
      // 医院匹配
      const hospitalMatch = !currentHospital || 
        expert.hospital.includes(currentHospital);
      
      return keywordMatch && specialtyMatch && hospitalMatch;
    });
    
    this.setData({
      filteredExperts: filtered
    });
  },
  
  // 查看专家详情
  viewDetail: function(e) {
    const expertId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/vp_pz/pages/expert/detail?id=${expertId}`
    });
  },
  
  // 下拉加载更多
  onReachBottom: function() {
    if (!this.data.isLoading && this.data.hasMore) {
      this.loadExperts();
    }
  }
});